package jml.plugins.justadded;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.StringTokenizer;

public class ProcessSplit
{
	private int nDays;
	private LinkedHashMap<String,String> splitDays;
	
	public ProcessSplit(int nDays, String toSplit)
	{
		this.nDays = nDays;
		splitDays = new LinkedHashMap<String,String>();

		StringTokenizer st = new StringTokenizer(toSplit, "|");
		while(st.hasMoreTokens())
		{
			String s = st.nextToken();
			int posComa = s.indexOf(',');
			int difDays = Integer.parseInt(s.substring(0, posComa));
			
			String sublabel = s.substring(posComa + 1);
			
			splitDays.put(Integer.toString(difDays), sublabel);
		}
	}
	
	public String[] getSplitted()
	{
		int i=0;
		String [] s = new String[splitDays.size()];
		for(Iterator<String> e=splitDays.keySet().iterator();e.hasNext();)
		{
			String k = e.next();
			s[i] = splitDays.get(k);
			i++;
		}

		return s;		
	}

	public String getLabelForDiff(int diff)
	{
		String k = null;
		int n = 0;
		for(Iterator<String> e=splitDays.keySet().iterator();e.hasNext();)
		{
			k = e.next();
			n = Integer.parseInt(k);
			if(diff <= n)
			{
				return splitDays.get(k);
			}
		}
		
		return diff<=nDays?splitDays.get(k):null;

	}

	public static void main(String[] args)
	{
////		ProcessSplit p = new ProcessSplit("0,Hoy|1,Ayer|2,Anteayer|6,Esta semana|13,La semana pasada|20,Hace 2 semanas|26,Hace 3 semanas|30,Hace mas de 3 semanas...ultimos dias aqui");
//
//		for(int i = 0; i < 32; i++)
//		{
//			System.out.println("dif=" + i + " dias: " + p.getLabelForDiff(i));
//		}
	}
}
